package com.tangbuzhi.builder;

/**
 * 建造者模式
 */
public class Builder {
    public static class Computer {
        private String mCpu;
        private String mMainBoard;
        private String mRam;

        public void setmCpu(String mCpu) {
            this.mCpu = mCpu;
        }

        public void setmMainBoard(String mMainBoard) {
            this.mMainBoard = mMainBoard;
        }

        public void setmRam(String mRam) {
            this.mRam = mRam;
        }

        @Override
        public String toString() {
            return mCpu + "==" + mMainBoard + "==" + mRam;
        }
    }

    public interface ComputerBuilder {
        void buildCpu(String cpu);

        void buildMainBoard(String mainBoard);

        void buildRam(String ram);

        Computer create();
    }

    public static class ConcreteBuilder implements ComputerBuilder {
        private Computer computer = new Computer();

        @Override
        public void buildCpu(String cpu) {
            computer.setmCpu(cpu);
        }

        @Override
        public void buildMainBoard(String mainBoard) {
            computer.setmMainBoard(mainBoard);
        }

        @Override
        public void buildRam(String ram) {
            computer.setmRam(ram);
        }

        @Override
        public Computer create() {
            return computer;
        }
    }

    public static class Director {
        private ComputerBuilder builder;

        public Director(ComputerBuilder builder) {
            this.builder = builder;
        }

        public Computer createComputer(String cpu, String mainBoard, String ram) {
            builder.buildCpu(cpu);
            builder.buildMainBoard(mainBoard);
            builder.buildRam(ram);
            return builder.create();
        }
    }

    public static void main(String[] args) {
        ComputerBuilder builder = new ConcreteBuilder();
        Director cBuilder = new Director(builder);
        Computer computer = cBuilder.createComputer("i7-6700", "酷睿i7", "骁龙800");
        System.out.println(computer.toString());
    }
}
